<?php
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/AddressList/ 地址列表获取
 * @apiName AddressList
 * @apiGroup Address
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/AddressDef/ 设置默认地址
 * @apiName AddressDef
 * @apiGroup Address
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} address_id 地址id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/AddressDel/ 删除地址
 * @apiName AddressDel
 * @apiGroup Address
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} address_id 地址id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/AddressAdd/ 新增地址
 * @apiName AddressAdd
 * @apiGroup Address
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiParam {Number} consignee 收件人(必填).
 * @apiParam {Number} mobile 收件人手机(必填).
 * @apiParam {Number} address 详细地址(必填).
 * @apiParam {Number} default 当为1时为默认地址(非必填).
 * @apiParam {Number} citylist 8-8-8-8 省-市-区-镇(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/AddressDefault/ 获取用户默认地址
 * @apiName AddressDefault
 * @apiGroup Address
 *
 * @apiParam {Number} user_id 用户id(必填).
 * @apiSuccess {Number} code  返回code(大于0为成功).
 */
/**
 * @api {get} http://aicomm.bndvip.com/Api/Address/cityList/ 城市列表获取
 * @apiName cityList
 * @apiGroup Address
 *
 * @apiParam {Number} city 城市id/获取省级城市为0(必填).
 * @apiParam {Number} level
 * -  1省/直辖市
 * -  2市
 * -  3区
 * -  4镇
 * @apiSuccess {Number} code  返回code(大于0为成功).
 * @apiSuccess {Array}  data  返回数组列表.
 */
namespace app\api\controller;
use think\Db;
use think\Cache;
use \think\Log;
class Address extends Base
{
    //新增用户地址列表
    public function AddressList(){
        $result = $this->request->param();
        Log::api('【Address/AddressList】-[参数]--'.json_encode($result));
        //获取用户user_id
        if ($result['user_id'] <=0 || empty($result['user_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        $getcity = Cache::get('city_1');
        if (empty($getcity)){
            $city = M('Region')->column('name','id');
            $getcity = Cache::set('city_1',$city,100000);
        }
        $where['user_id'] = array('eq',$result['user_id']);
        $where['is_del'] = array('eq',0);
        $list = M('user_address')->where($where)->select();
        $addList = array();
        foreach ($list as $k=>$v){
            $addList[$k] = $v;
            $addList[$k]['country_name'] = "中国";
            $addList[$k]['province_name'] = $getcity[$v['province']];
            $addList[$k]['city_name'] = $getcity[$v['city']];
            $addList[$k]['district_name'] = $getcity[$v['district']];
            $addList[$k]['twon_name'] = $getcity[$v['twon']];
        }
        if ($addList){
            $return = codemsg(3);
            $return['data'] = $addList;
            Log::api('【Address/AddressList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7018);
            Log::api('【Address/AddressList】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //设置默认地址
    public function AddressDef(){
        $result = $this->request->param();
        Log::api('【Address/AddressDef】-[参数]--'.json_encode($result));
        //获取用户user_id
        if ($result['user_id'] <=0 || empty($result['user_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressDef】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['address_id'] <=0 || empty($result['address_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressDef】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //将该用户下的所有状态改为非默认状态
        $map['is_default'] = 0;
        $where['user_id'] = $result['user_id'];
        $update = M('user_address')->where($where)->update($map);
        $map['is_default'] = 1;
        $where['address_id'] = $result['address_id'];
        $seva = M('user_address')->where($where)->update($map);
        if ($seva){
            $return = codemsg(5004);
            Log::api('【Address/AddressDef】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7019);
            Log::api('【Address/AddressDef】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //删除地址
    public function AddressDel(){
        $result = $this->request->param();
        Log::api('【Address/AddressDel】-[参数]--'.json_encode($result));
        //获取用户user_id
        if ($result['user_id'] <=0 || empty($result['user_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressDel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        if ($result['address_id'] <=0 || empty($result['address_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressDel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //将该用户下的所有状态改为非默认状态
        $map['is_del'] = 1;
        $where['address_id'] = $result['address_id'];
        $save = M('user_address')->where($where)->update($map);
        if ($save){
            $return = codemsg(5005);
            Log::api('【Address/AddressDel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7017);
            Log::api('【Address/AddressDel】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //新增地址
    public function AddressAdd(){
        $result = $this->request->param();
        Log::api('【Address/AddressAdd】-[参数]--'.json_encode($result));
        //获取用户user_id
        if ($result['user_id'] <=0 || empty($result['user_id'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //获取省市区 citylist 8-8-8-8 省-市-区-镇
        if (empty($result['citylist']) || empty($result['consignee']) || empty($result['mobile']) || empty($result['address'])){
            //用户id不能为空
            $return = codemsg(-1);
            Log::api('【Address/AddressAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
        //账号--手机号码
        $checkphone = regcheck('phone',$result['mobile']);
        if ($checkphone){
            $return = $checkphone;
            Log::login('【Address/AddressAdd】-[return]--'.json_encode($return));
            echo $return;die;
        }
        $citylist = explode('-',$result['citylist']);
        //判断用户是否存在设置默认地址
        if ($result['default'] == 1){
            //当参数default为1时设置为默认地址
            $map['is_default'] = 1;
            $mapde['is_default'] = 0;
            //查询是否存在有设置默认地址
            $dewhere['user_id'] = array('eq',$result['user_id']);
            $dewhere['is_del'] = array('eq',0);
            $dewhere['is_default'] = array('eq',1);
            $finddefault = M('user_address')->where($dewhere)->find();
            if ($finddefault){
                $update = M('user_address')->where($dewhere)->update($mapde);
            }
        }
        $map['country'] = 0;
        $map['province'] = $citylist['0'];
        $map['city'] = $citylist['1'];
        $map['district'] = $citylist['2'];
        $map['twon'] = '';
        $map['address'] = $result['address'];
        $map['mobile'] = $result['mobile'];
        $map['consignee'] = $result['consignee'];
        $map['user_id'] = $result['user_id'];
        $add = M('user_address')->add($map);
        if ($add){
            $return = codemsg(3);
            Log::api('【Address/AddressAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(-7007);
            Log::api('【Address/AddressAdd】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }
    }

    //默认地址获取
    public function AddressDefault(){
        $user_id = I('user_id');
        //通过用户uid获取默认地址
        $where['user_id'] = array('eq',$user_id);
        $where['is_default'] = array('eq',1);
        $find = M('user_address')->where($where)->find();
        if (empty($find)){
            $iswhere['user_id'] = array('eq',$user_id);
            $find = M('user_address')->where($iswhere)->find();
        }
        if (empty($find)){
            //空
            $return = codemsg(-7028);
            Log::api('【Address/AddressDefault】-[返回]--'.json_encode($return));
            echo json_encode($return);die;
        }else{
            $return = codemsg(5003);
            Log::api('【Address/AddressDefault】-[返回]--'.json_encode($return));
            $return['data'] = $find;
            echo json_encode($return);die;
        }
    }

    //查找城市，小区等
    public function cityList(){
        //获取省市级 city 城市id 0为省级城市 level 为级别1为省级
        $result = $this->request->param();
        if ($result['city'] == 0 && $result['level'] == 1){
            $where['level'] = 1;
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 2){
            $where['level'] = 2;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] == 3){
            $where['level'] = 3;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }elseif ($result['level'] = 4){
            $where['level'] = 4;
            $where['parent_id'] = $result['city'];
            $data = M('Region')->where($where)->select();
        }else{
            $data = '';
        }
        if ($data){
            $return = codemsg(3);
            $return['data'] = $data;
        }else{
            $return = codemsg(-7000);
        }
        echo json_encode($return);
    }
}